package com.wifi.eshipewifi;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.wifi.BaseApplication;
import com.wifi.imp.MessageListener;
import com.wifi.mvp.presenter.imp.MainPresenter;
import com.wifi.mvp.view.MainView;

import butterknife.BindView;

//mvp规范类
public class MainActivity extends BaseActivity implements View.OnClickListener,MessageListener,MainView{

    @BindView(R.id.rl_sign)
    RelativeLayout rl_sign;
    @BindView(R.id.rl_wifi)
    RelativeLayout rl_wifi;
    @BindView(R.id.rl_conn)
    RelativeLayout rl_conn;
    @BindView(R.id.rl_cjh)
    RelativeLayout rl_cjh;
    @BindView(R.id.rl_cz)
    RelativeLayout rl_cz;
    @BindView(R.id.rl_smb)
    RelativeLayout rl_smb;
    @BindView(R.id.rl_package_setting)
    RelativeLayout rl_package_setting;
    @BindView(R.id.ll_package)
    LinearLayout ll_package;
    @BindView(R.id.tv_setting)
    ImageView tv_setting;
    @BindView(R.id.tv_jiaozheng)
    TextView tv_jiaozheng;
    @BindView(R.id.tv_sim_tittle)
    TextView tv_sim_tittle;
    @BindView(R.id.tv_wifi_name)
    TextView tv_wifi_name;
    @BindView(R.id.tv_size)
    TextView tv_size;
    @BindView(R.id.tv_sign_name)
    TextView tv_sign_name;
    @BindView(R.id.tv_conn_num)
    TextView tv_conn_num;
    @BindView(R.id.tv_sign_state)
    TextView tv_sign_state;
    @BindView(R.id.tv_liul)
    TextView tv_liul;
    @BindView(R.id.tv_remain_liul)
    TextView tv_remain_liul;
    @BindView(R.id.pb_sign)
    ImageView pb_sign;
    @BindView(R.id.pb_conn)
    ProgressBar pb_conn;
    @BindView(R.id.pb_package)
    ProgressBar pb_package;

    String TAG="MainActivity";
    String ip="192.168.8.1";
    BaseApplication application;
    SharedPreferences sp;
    boolean isNetWockOpen=false;//移动数据标志位
    MainPresenter mainPresenter;
    String YYS;//运营商

    @Override
    protected int getContentLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void onResume() {
        super.onResume();
//todo    进入第一次就校正流量
        if(null==mainPresenter)
            mainPresenter=new MainPresenter();
        application = mainPresenter.getmApplication(this);
        mainPresenter.setListener(this);
        mainPresenter.setMessage("$init;");
        ////        handler.postDelayed(task,5000);//todo 延迟调用,测试用
    }

    @Override
    protected void initViewsAndEvents() {
        sp= getSharedPreferences("package", Activity.MODE_PRIVATE);
        tv_jiaozheng.setOnClickListener(this);
        tv_remain_liul.setOnClickListener(this);
        rl_cjh.setOnClickListener(this);
        rl_package_setting.setOnClickListener(this);
        rl_cz.setOnClickListener(this);
        rl_smb.setOnClickListener(this);
        ll_package.setOnClickListener(this);
        rl_conn.setOnClickListener(this);
        rl_sign.setOnClickListener(this);
        rl_wifi.setOnClickListener(this);
        tv_setting.setOnClickListener(this);
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_smb:
                readyGo(PlayfileActivity.class);
                break;
            case R.id.rl_cjh:
                readyGo(WebViewActivity.class);
                break;
            case R.id.tv_setting:
                readyGo(SettingActivity.class);
                break;
            case R.id.rl_sign:
                readyGo(NetWorkSetting.class);
                break;
            case R.id.rl_conn:
                readyGo(ConnetAcitvity.class);
                break;
            case R.id.rl_wifi:
                application.makeToast(MainActivity.this,"桥接功能暂不开放",true);
//                readyGo(NetWorkConActivity.class);
                break;
            case R.id.rl_cz:
                readyGo(PayActivity.class);
                break;
            case R.id.ll_package:
                readyGo(PackageSettingActivity.class);
                break;
            case R.id.tv_remain_liul:
                readyGo(PackageCurrSettingActivity.class);
                break;
            case R.id.tv_jiaozheng:
                final View view = View.inflate(MainActivity.this, R.layout.item_dialog_check, null);
                mainPresenter.showAlertDialog(view,MainActivity.this,"流量校正使用注意事项","$check_data;");
                break;
        }
    }

    //数据处理 todo 应写一个bean来处理数据
    @Override
    public void MessageGetListener(String msg) {
        try {
            if (msg != null) {
                String s = msg;
                if (s.trim().length() > 0) {
                    Log.i(TAG, "mhandler接收到obj=" + s);
                    pb_conn.setVisibility(View.GONE);//扫描pb
                    String s1[]=s.split(";");
                    if(s1[0].indexOf("init")!=-1){//首页数据
//   $init;CHINA UNICOM,4G,80;1;4g_on;sim1,0MB,0GB;WiFi_off;
                        if(s1.length>4){
                            application.makeToast(MainActivity.this,s1[1],false);
                            //---连接数量
                            String conn_name=s1[2];
                            String s2[]=conn_name.split(",");
                            tv_conn_num.setText(conn_name);
                            //---运营商
                            String[] s_4g = s1[1].split(",");
                            if(s_4g[0].indexOf("MOBILE")!=-1){//中国移动
                                YYS="中国移动";
                                tv_sign_name.setText("中国移动");
                            }else  if(s_4g[0].indexOf("UNICOM")!=-1){//中国联通
                                YYS="中国联通";
                                tv_sign_name.setText("中国联通");
                            }else  if(s_4g[0].indexOf("TELECOM")!=-1){//中国电信
                                YYS="中国电信";
                                tv_sign_name.setText("中国电信");
                            }else  if(s_4g[0].equals("")){//路由端会出现空异常，取上一次的运营商数据
                                String yys = sp.getString("YYS", "中国联通");
                                YYS=yys;
                                tv_sign_name.setText(yys);
                            }else  if(s_4g[0].equals("no signal")){//路由端会出现空异常，取上一次的运营商数据
                                YYS="无信号";
                                tv_sign_name.setText("无信号");
                            }else {
                                YYS="虚拟运营商";
                                tv_sign_name.setText("虚拟运营商");
                            }
                            //---信号
                            Integer int_4g = Integer.valueOf(s_4g[2]);
                            String sign = s_4g[1];
                            if(sign.indexOf("4G")!=-1){//4G
                                if(int_4g>-1 && int_4g<25){
                                    pb_sign.setBackgroundResource(R.drawable.g4_1);
                                }else if(int_4g>24 && int_4g<50){
                                    pb_sign.setBackgroundResource(R.drawable.g4_2);
                                }else if(int_4g>49 && int_4g<75){
                                    pb_sign.setBackgroundResource(R.drawable.g4_3);
                                }else if(int_4g>74 && int_4g<101){
                                    pb_sign.setBackgroundResource(R.drawable.g4_4);
                                }else {
                                    pb_sign.setBackgroundResource(R.drawable.g4_1);
                                }
                                //todo wifi 变色逻辑 4g开关
                                if(s1[3].indexOf("off")!=-1){
                                    isNetWockOpen=false;
                                    tv_sign_state.setText("4G  未连接");
                                    pb_sign.setBackgroundResource(R.drawable.sign_out);
                                    rl_sign.setBackgroundResource(R.drawable.linearlayout_selector);//未选中状态
                                }else {
                                    isNetWockOpen=true;
                                    tv_sign_state.setText("4G  已连接");
                                    rl_sign.setBackgroundResource(R.drawable.blue_background);//选中状态
                                }
                            }else if(sign.indexOf("3G")!=-1){//3G
                                if(int_4g>-1 && int_4g<25){
                                    pb_sign.setBackgroundResource(R.drawable.g3_1);
                                }else if(int_4g>24 && int_4g<50){
                                    pb_sign.setBackgroundResource(R.drawable.g3_2);
                                }else if(int_4g>49 && int_4g<75){
                                    pb_sign.setBackgroundResource(R.drawable.g3_3);
                                }else if(int_4g>74 && int_4g<101){
                                    pb_sign.setBackgroundResource(R.drawable.g3_4);
                                }else {
                                    pb_sign.setBackgroundResource(R.drawable.g3_1);
                                }
                                //3g开关
                                if(s1[3].indexOf("off")!=-1){
                                    isNetWockOpen=false;
                                    tv_sign_state.setText("3G  未连接");
                                    pb_sign.setBackgroundResource(R.drawable.sign_out);
                                    rl_sign.setBackgroundResource(R.drawable.linearlayout_selector);//未选中状态
                                }else {
                                    isNetWockOpen=true;
                                    tv_sign_state.setText("3G  已连接");
                                    rl_sign.setBackgroundResource(R.drawable.blue_background);//选中状态
                                }
                            }else if(sign.indexOf("2G")!=-1){//2G
                                if(int_4g>-1 && int_4g<25){
                                    pb_sign.setBackgroundResource(R.drawable.g2_1);
                                }else if(int_4g>24 && int_4g<50){
                                    pb_sign.setBackgroundResource(R.drawable.g2_2);
                                }else if(int_4g>49 && int_4g<75){
                                    pb_sign.setBackgroundResource(R.drawable.g2_3);
                                }else if(int_4g>74 && int_4g<101){
                                    pb_sign.setBackgroundResource(R.drawable.g2_4);
                                }else {
                                    pb_sign.setBackgroundResource(R.drawable.g2_1);
                                }
                                //4g开关
                                if(s1[3].indexOf("off")!=-1){
                                    isNetWockOpen=false;
                                    tv_sign_state.setText("2G  未连接");
                                    pb_sign.setBackgroundResource(R.drawable.sign_out);
                                    rl_sign.setBackgroundResource(R.drawable.linearlayout_selector);//未选中状态
                                }else {
                                    isNetWockOpen=true;
                                    tv_sign_state.setText("2G  已连接");
                                    rl_sign.setBackgroundResource(R.drawable.blue_background);//选中状态
                                }
                            }else {
                                tv_sign_state.setText("无信号");
                                pb_sign.setBackgroundResource(R.drawable.sign_out);
                                rl_sign.setBackgroundResource(R.drawable.linearlayout_selector);//未选中状态
                            }

                            //---已使用流量
//                            liuliang_package = sp.getString("packageSetting", "4");//设置的套餐流量
                            String[] data=s1[4].split(",");
                            String sim=data[0];
                            String liulang=data[1];
                            String package_liuliang=data[2];
                            boolean isGB=false;
                            boolean curr_isGB=false;
                            //保存最新套餐流量
                            if(package_liuliang.indexOf("MB")!=-1){
                                package_liuliang=package_liuliang.replace("MB","");//去除单位
                                tv_size.setText("MB");
                                isGB=false;
                            }else if(package_liuliang.indexOf("GB")!=-1){
                                package_liuliang=package_liuliang.replace("GB","");//去除单位
                                tv_size.setText("GB");
                                isGB=true;
                            }
                            //根据sim切换界面
                            if(sim.indexOf("sim1")!=-1){//sim卡1
                                tv_jiaozheng.setVisibility(View.VISIBLE);
                                tv_sim_tittle.setText("SIM卡1已使用流量");
                            }else {//sim卡2
                                tv_jiaozheng.setVisibility(View.GONE);
                                tv_sim_tittle.setText("SIM卡2已使用流量");
                            }
                            tv_liul.setText(package_liuliang);
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putString("packageSetting",package_liuliang);
                            edit.putBoolean("isGB",isGB);
                            edit.putString("sim",sim);


                            //流量判断,进度条展示
                            Drawable draw;
                            if(liulang.indexOf("MB")!=-1){//使用流量为MB

                                liulang=liulang.replace("MB","");
                                Double liuliang_package1 = Double.valueOf(package_liuliang.trim());
                                Double liulang1 = Double.valueOf(liulang.trim());
                                curr_isGB=false;
                                edit.putBoolean("curr_isGB",curr_isGB);
                                edit.putString("curr_ll",String.valueOf(liulang1));
                                   if(isGB){//总流量为GB
                                       pb_package.setMax((int)Math.floor(( liuliang_package1)*1024));
                                       if(liulang1/(liuliang_package1*1024)>0.9) {//超出预警流量
                                           if(liulang1>liuliang_package1){//超出流量
                                               pb_package.setProgress((int)Math.floor(liuliang_package1)*1024);
                                               application.makeToast(MainActivity.this,"流量已超出套餐限制，建议关闭4G或充值",true);
                                           }else {//超出预警流量提示
                                               pb_package.setProgress((int)Math.floor(liulang1));
                                               application.makeToast(MainActivity.this,"流量已使用90%，建议关闭4G或充值",true);
                                           }
                                           draw = getResources().getDrawable(R.drawable.processbar_color_red);
                                       }else {//正常流量展示
                                           pb_package.setProgress((int)Math.floor(liulang1));
                                           draw = getResources().getDrawable(R.drawable.processbar_color);
                                       }
                                       pb_package.setProgressDrawable(draw);
                                   }else {//总流量为MB
                                       pb_package.setMax((int)Math.floor((liuliang_package1)));
                                       if(liulang1/liuliang_package1>0.9) {//超出预警流量
                                           if(liulang1>liuliang_package1){//超出流量
                                               application.makeToast(MainActivity.this,"流量已超出套餐限制，建议关闭4G或充值",true);
                                               pb_package.setProgress((int)Math.floor(liuliang_package1));
                                           }else {
                                               pb_package.setProgress((int)Math.floor(liulang1));
                                               application.makeToast(MainActivity.this,"流量已使用90%，建议关闭4G或充值",true);
                                           }
                                           draw = getResources().getDrawable(R.drawable.processbar_color_red);
                                       }else {//正常流量展示
                                           pb_package.setProgress((int)Math.floor(liulang1));
                                           draw = getResources().getDrawable(R.drawable.processbar_color);
                                       }
                                   }
                                tv_remain_liul.setText(liulang+"MB");
                                pb_package.setProgressDrawable(draw);
                            }else if(liulang.indexOf("GB")!=-1){//使用流量为GB
                                liulang=liulang.replace("GB","");
                                Double liuliang_package1 = Double.valueOf(package_liuliang);
                                Double liulang1 = Double.valueOf(liulang.trim());
                                curr_isGB=true;
                                edit.putBoolean("curr_isGB",curr_isGB);
                                edit.putString("curr_ll",String.valueOf(liulang1));
                                if(isGB){//总流量为GB
                                    pb_package.setMax((int)Math.floor(( liuliang_package1)*1024));
                                    if(liulang1/liuliang_package1>0.9){//90%流量预警
                                        if(liulang1>liuliang_package1) {//超出流量
                                            pb_package.setProgress((int) Math.floor(liulang1) * 1024);
                                            application.makeToast(MainActivity.this,"流量已超出套餐限制，建议关闭4G或充值", true);
                                        }else {//超出预警流量提示
                                            pb_package.setProgress((int) Math.floor(liuliang_package1) * 1024);
                                            application.makeToast(MainActivity.this,"流量已使用90%，建议关闭4G或充值" , true);
                                        }
                                        //进度条转为红色
                                        draw = getResources().getDrawable(R.drawable.processbar_color_red);
                                    }else {
                                        pb_package.setProgress((int)Math.floor(liulang1*1024));
                                        draw = getResources().getDrawable(R.drawable.processbar_color);
                                    }
                                }else {//总流量为MB,使用流量为GB
                                    if((liulang1*1024)/liuliang_package1>0.9){//90%流量预警
                                        if((liulang1*1024)>liuliang_package1) {//超出流量
                                            pb_package.setProgress((int) Math.floor(liulang1) * 1024);
                                            application.makeToast(MainActivity.this,"流量已超出套餐限制，建议关闭4G或充值", true);
                                        }else {//超出预警流量提示
                                            pb_package.setProgress((int) Math.floor(liuliang_package1) * 1024);
                                            application.makeToast(MainActivity.this,"流量已使用90%，建议关闭4G或充值" , true);
                                        }
                                        //进度条转为红色
                                        draw = getResources().getDrawable(R.drawable.processbar_color_red);
                                    }else {
                                        pb_package.setProgress((int)Math.floor(liulang1*1024));
                                        draw = getResources().getDrawable(R.drawable.processbar_color);
                                    }
//                                    pb_package.setMax((int)Math.floor(( liuliang_package1)));
//                                    pb_package.setProgress((int)Math.floor(liuliang_package1));
//                                    application.makeToast(MainActivity.this,"流量已超出套餐限制，建议关闭4G或充值",true);
//                                    draw = getResources().getDrawable(R.drawable.processbar_color_red);
                                }
                                tv_remain_liul.setText(liulang+"GB");
                                pb_package.setProgressDrawable(draw);
                            }
                            edit.commit();//保存当前使用流量、套餐流量、当前sim卡

                            //wifi开关
                            String wifi=s1[5];
                            SharedPreferences.Editor wifi_edit = sp.edit();
                            if(wifi.indexOf("off")!=-1){//关闭
                                tv_wifi_name.setText("wifi未连接");
                                rl_wifi.setBackgroundResource(R.drawable.linearlayout_selector);//未选中状态
                                wifi_edit.putBoolean("isWifiOpen",false);
//                                isWifiOpen=false;
                            }else {//打开
                                tv_wifi_name.setText("wifi已连接");
                                rl_wifi.setBackgroundResource(R.drawable.blue_background);//选中状态
                                wifi_edit.putBoolean("isWifiOpen",true);
//                                isWifiOpen=true;
                            }
                            wifi_edit.commit();
                        }
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putBoolean("isNetWockOpen",isNetWockOpen);
                        edit.putString("YYS",YYS);
                        edit.commit();

                    }else if(s1[0].indexOf("first_connect")!=-1){
                        application.makeToast(MainActivity.this,"连接成功,请稍等",true);
                    }else if(s1[0].indexOf("check_data")!=-1){
//                        $check_data;71.345MB,7.789MB;\r\n " --总流量和已使用流量
//                        $check_data; service_unavailabe;\r\n " --服务器不可用
//                        $check_data; phoneNumber_wrong;\r\n " --手机号错误
//                        $check_data; network_unavailabe;\r\n " --网络不通
                        if(s1[1].indexOf("service_unavailabe")!=-1){
                            application.makeToast(MainActivity.this,"服务器连接失败，请重试",true);
                        }else  if(s1[1].indexOf("phoneNumber_wrong")!=-1){
                            application.makeToast(MainActivity.this,"手机号错误，请校正手机号",true);
                        }else  if(s1[1].indexOf("network_unavailabe")!=-1){
                            application.makeToast(MainActivity.this,"无网络，请开启4G或开启WIFI连接",true);
                        }else {
                            application.makeToast(MainActivity.this,"校正流量为："+s1[1],true);
                        }
                        application.sendMsg("$init;");
                    }
                }
            }

            Log.i(TAG, "mhandler接收到msg" + msg);
        } catch (Exception ee) {
            Log.i(TAG, "加载过程出现异常");
            ee.printStackTrace();
        }
    }

    private Handler handler = new Handler();

    private Runnable task =new Runnable() {
        public void run() {
            // TODOAuto-generated method stub
            handler.postDelayed(this,5*1000);//设置延迟时间，此处是5秒
            //需要执行的代码
            application.sendMsg("$init;");
        }
    };


    @Override
    public void onSuccess() {

    }

    @Override
    public void onEorr() {

    }
}
